var PreOpen = new Image();
var PreClose = new Image();
var PreCopy = new Image();
PreOpen.src="/images/plus.gif";
PreClose.src = "/images/minus.gif";
PreCopy.src = "/images/copy_16.png";
var canCopy = document.all;

function togglePre()
{
	var id = this.getAttribute("preid");
	var preelm = document.getElementById("pre" + id);
	var imgelm = document.getElementById("preimg" + id);
	var togelm = document.getElementById("precollapse" + id);

	if (preelm.style.display != 'none') 
	{
		if (document.all) togelm.innerText = " Expand code";
		else {
			document.getElementById("premain" + id).className = "small-text collapsible align-right";
			togelm.firstChild.nodeValue = " Expand code";
		}
		preelm.style.display = 'none';
		imgelm.setAttribute("src", PreOpen.src);
	}
	else 
	{
		if (document.all) togelm.innerText = " Collapse code";
		else {	
			document.getElementById("premain" + id).className = "small-text align-right";
			togelm.firstChild.nodeValue = " Collapse code";
		}
		preelm.style.display = 'block';
		imgelm.setAttribute("src", PreClose.src);
	}
}

function CopyCode(name) {
	if (!canCopy) return false;

	var id = this.getAttribute("preid");
	if (id < 0) return false;
	
	var elm = document.getElementById("pre" + id.toString());
	if (!elm) return false;
	
	var text = elm.innerHTML;
	if (!text) return;
	text = prepareCopyText(text);

	window.clipboardData.setData("Text", text);

	return false;
}

function InitTogglePre() {
	var articleText = document.getElementById("contentdiv");
	if (!articleText) return;
	var pres = articleText.getElementsByTagName("pre");

	if (window.netscape) {
		try { netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); }
		catch (e) {canCopy = false; }
	}
	
	for (var i=0; i<pres.length; i++) {
		if (pres[i].decoration && pres[i].decoration.toLowerCase() == 'none')
			continue;

		var parent = pres[i].parentNode;
		var length = pres[i].innerHTML.length;

		var lines = length - pres[i].innerHTML.replace(/\n/ig,'').length +
		            (length - pres[i].innerHTML.replace(/\<br/ig, '').length) / 3;

		// if (lines < 10) continue;
		
		var main = document.createElement("div");
		main.className = "small-text align-right";
		main.setAttribute("id", "premain" + i.toString());
		main.setAttribute("width", "100%");
		main.setAttribute("style", "display:block");

		var elm = document.createElement("img");
		elm.setAttribute("id", "preimg" + i.toString());
		elm.setAttribute("src", PreClose.src);
		elm.style.cursor = "pointer";
		elm.setAttribute("height", 9);
		elm.setAttribute("width", 9);
		elm.setAttribute("preid", i);
		elm.onclick = togglePre;

		main.appendChild(elm);	
	
		elm = document.createElement("span");
		elm.setAttribute("id", "precollapse" + i.toString());
		elm.style.cursor = "pointer";
		elm.style.marginBottom = 0;
		elm.onclick = togglePre;
		elm.setAttribute("preid", i);
		setText(elm, " Collapse");
	
		main.appendChild(elm);

		if (canCopy) {
			 elm = document.createElement("span");
			 setText(elm, " | ");
			 main.appendChild(elm);

			/*elm = document.createElement("img");
			elm.setAttribute("src", PreCopy.src);
			elm.setAttribute("height", 16);
			elm.setAttribute("width", 16)
			elm.style.marginLeft = "35px";
			main.appendChild(elm);*/

			elm = document.createElement("a");
			setText(elm, "Copy Code");

			elm.href = '#';
			elm.setAttribute("preid", i);
			elm.onclick = CopyCode;
			main.appendChild(elm);
		}
		
		pres[i].setAttribute("id", "pre" + i.toString());
		pres[i].style.marginTop = 0;
		
		var parent = pres[i].parentNode;
		parent.insertBefore(main, pres[i]);	
	}
}

function prepareCopyText(code) {
	if (!code) return null;
	// Replace superfluous <br> tags in a <pre> block.
	var re = /<br[^>]*>/gi;
	var code = code.replace(re, "\r\n");

	// Replace non-breaking spaces.
	re = /&nbsp;/gi;
	code = code.replace(re, " ");

	// Replace & characters.
	re = /&amp;/gi;
	code = code.replace(re, "&");

	// Replace < characters.
	re = /&lt;/gi;
	code = code.replace(re, "<");

	// Replace > characters.
	re = /&gt;/gi;
	code = code.replace(re, ">");

	// CJM: Remove colorization
	re = /<\/?span([^>])*>/gi;
	code = code.replace(re, "");

	return code;
}
function setText(elm, text) {
	if (typeof elm.innerText != 'undefined') elm.innerText = text; else elm.textContent = text;
}

InitTogglePre();